/*-----------------------------
----$Source: Volumes://MATEOR/vais/modules/init/init.c$
----$Date: June 30, 2012$
-------------------------------
VAIS Copyright Matthew Caswell, 2012
-----------------------------*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vais.h>
#include<lmath.h>
#include<lstring.h>
#include<lds_ts.h>
#include<init.h>
#include<time.h>
#include<braindo.h>
#include<pthread.h>
#include<lserver.h>
#include<unistd.h>

void* VAIS_Directive (void* path){
  execl ((char*)path, "0", (char*)0);
  debug ("Execute directive", "VAIS_Directive", "init/init.c");

  return (null);
}

void initDataStream (VAIS_DS_t* DS, char* DS_Record){
  FILE* DS_FILE;
  
  debug ("initDataStream", "initDataStream", "init/init.c");
  
  char c[DS_MD];
  int n = 0;
  
  DS_FILE = fopen (DS_Record, "r");
  
  while(!(feof (DS_FILE))){
    c[n] = (char)fgetc (DS_FILE);
    (*DS).dataStream[n] = c[n];
	debug_c (c[n], "initDataStream", "init/init.c");
    n++;
  }
  
  fclose (DS_FILE);
}

void sortDataStream (char str[DS_MD], char* c[DS_MD], VAIS_DS_t* DS){
  int n = 0;
  
  c[n] = strtok (str, "\n");
  debug (c[n], "sortDataStream", "init/init.c");
  n++;
  while ((c[n] = strtok (null, "\n"))){
    debug (c[n], "sortDataStream", "init/init.c");
    n++;
  }
  
  (*DS).index = n;
}

void parseDataStream (VAIS_DS_t* DS, int index){
  sscanf ((*DS).dataSeg[index], "%s %s %s %5c", (*DS).first[index], (*DS).second[index], (*DS).third[index], (*DS).fourth[index]);
  debug ("%s %s %s %s", "parseDataStream", "init/init.c");
}

void h_DataStream (VAIS_DS_t* DS, char* DS_Record){
  initDataStream (DS, DS_Record);
  sortDataStream ((*DS).dataStream, (*DS).dataSeg, DS);
  
  int n = 0;
  
  for(; n<(*DS).index; n++){
    parseDataStream (DS, n);
    debug ((*DS).first[n], "h_DataStream", "init/init.c");
    debug ((*DS).second[n], "h_DataStream", "init/init.c");
    debug ((*DS).third[n], "h_DataStream", "init/init.c");
    debug ((*DS).fourth[n], "h_DataStream", "init/init.c");  
  }
}

void* VAIS_init (void* DS_Record){
  
  VAIS_DS_t* DS = malloc (sizeof (*DS));
  h_DataStream (DS, DS_Record);
  
  pthread_t* threads;
  pthread_attr_t pthread_custom_attr;
  parm* p;
  
  threads = (pthread_t*)malloc (sizeof (parm) * MAX_THREAD);
  pthread_attr_init (&pthread_custom_attr);
  
  p = (parm*)malloc (sizeof (parm) * MAX_THREAD);
  
  int i = 0;
  int n,z;
  z = 0;
  char VAIS_TS_B[23];
  int run = 0;
  
  for(; i<(*DS).index; i++){
    debug ((*DS).first[i], "VAIS_init", "init/init.c");
    if(strcmp ((*DS).first[i], "CMD") == 0){
      debug ((*DS).second[i], "VAIS_init", "init/init.c");
      if(strcmp ((*DS).second[i], "EXPORT") == 0){
        if(strcmp ((*DS).fourth[i], "01001") == 0){
          time_t t = time (0);  // Recibe tiempo ahora
          struct tm* now = localtime (&t);
          debug_i ((now->tm_year - 100), "Year", "init/init.c");
          debug_i ((now->tm_mday), "Day", "init/init.c");
          debug_i ((now->tm_mon+1), "Month", "init/init.c");
          debug_i ((now->tm_hour), "Hour", "init/init.c");
          debug_i ((now->tm_min), "Minute", "init/init.c");
          debug_i ((((((((((now->tm_year - 100) * 100) + (now->tm_mon+1)) * 100) + (now->tm_mday)) * 100) + (now->tm_hour)) * 100) + (now->tm_min)), "DS", "init/init.c");
          n = sprintf(VAIS_TS_B, "PP%iDINITTIME0", (((((((((now->tm_year - 100) * 100) + (now->tm_mon+1)) * 100) + (now->tm_mday)) * 100) + (now->tm_hour)) * 100) + (now->tm_min)));
          
          FILE* DS_File;
          DS_File = fopen ((*DS).third[i], "w");
          fwrite (VAIS_TS_B, 1, sizeof (VAIS_TS_B), DS_File);
          debug ("VAIS_TS_B", "VAIS_init", "init/init.c");
          fclose (DS_File);
        }else{
          debug ("(*DS).fourth[i] != 01001", "VAIS_init", "init/init.c");
        }
      }else if(strcmp ((*DS).second[i], "BRAINDO") == 0){
        VAIS_braindo((*DS).third[i], run);
        if((run > 4) && (strcmp((*DS).third[i - 2], (*DS).third[i]) == 0))
          printf ("{-- BrainDo: Communication functions: %s : \e[1;31mNEURAL SIEZURE!!!\e[0m --}\n", (*DS).third[i]);
        
        if(strcmp((*DS).third[i - 2], (*DS).third[i]) == 0)
          run++;
          
        if((run > 9) && (strcmp((*DS).third[i - 2], (*DS).third[i]) == 0)){
          printf ("{-- BrainDo: Communication functions: %s : \e[1;31mNEURAL SIEZURE!!!\e[0m --}\n", (*DS).third[i]);
          error ("DANGER! DANGER! A NEURAL FUNCTION HAS BEEN EXECUTED CONTINUOUSLY MORE THAN 9 TIMES IN A ROW WITHOUT BYPASSING NEURAL PRESERVATION SYSTEMS!");
          error ("ABORTING CURRENT SUBCONSCIOUS PROCESS");
          return (null);
        }

      }else if(strcmp ((*DS).second[i], "RESETRUN") == 0){
        run = 0;
      }else if(strcmp ((*DS).second[i], "OPENSERV") == 0){
        p[z].id = z;
        pthread_create (&threads[z], &pthread_custom_attr, VAIS_Lobe_server, (void*)(*DS).third[i]);
        debug ("OPENSERV 0", "VAIS_init", "init/init.c");pthread_create (&threads[z], &pthread_custom_attr, VAIS_Lobe_server, (void*)(*DS).third[i]);
      }else if(strcmp ((*DS).second[i], "DIRECTIVE") == 0){
        p[z].id = z;
        pthread_create (&threads[z], &pthread_custom_attr, VAIS_Directive, (void*)(*DS).third[i]);
        debug ("DIRECTIVE 0", "VAIS_init", "init/init.c");
      }else{
        debug ("(*DS).second[i] != EXPORT", "VAIS_init", "init/init.c");
      }
    } else {
      debug ("(*DS).first[i] != CMD", "VAIS_init", "init/init.c");
    }
  }
  
  for(i = 0; i<z; i++){
    pthread_join (threads[i], null);
  }
  
  
  // ...

  return (null);
}
